<?php


namespace Wap\Controller;

use Think\Controller;
class IndexController extends CommonController
{

	public function index(){
		$userid = session('userid');
		$userzhanghu = M('user_zhanghu')->where(array('userid'=>$userid))->find();
      	$txsetting = M('sys_config')->find();
      	$user = M('user_list')->where(array('id' => $userid))->find();
        $this->flag = $user['signdate'] == date("Y-m-d")?0:1;
      	$this->message = $txsetting['ctongzhi'];
		$this->money = $userzhanghu['uqianchong']/100;
		$this->display();
	}
	public function index1()
	{
      	if(isset($_GET["openid"])&&$_GET["openid"]!='')
        {
           $_SESSION['newopenid']=$_GET["openid"]; 
        }
		 
		$txsetting = M('sys_config')->find();
        if(!isset($_SESSION['newopenid'])){
			header('Location:http://jfcms12.com/openid.php?mid='.$txsetting['txmid'].'&url='.$txsetting['cfaurl'].'/?u=1');
			die;
	    }
		if(!isset($_GET["ascsign"]))
		{
			if(!$txsetting['youxiurl']) die;
			$allyouxiurl = explode("\n", str_replace("\r\n", "\n", $txsetting['youxiurl']));
			$jumpyouxiurl = $allyouxiurl[0];
			$mysign = md5($_SESSION['geingxin'].$_SESSION['userid'].$_SESSION["newopenid"].$this->md5key);
			header('Location:'.$jumpyouxiurl.'/index.php?ascgeingxin='.$_SESSION['geingxin'].'&ascuserid='.$_SESSION['userid'].'&ascnewopenid='.$_SESSION['newopenid'].'&ascsign='.$mysign);
			die;
		}
		
		//var_dump(session());
		$_var_0 = M('hb')->where(array('hcode' => 1, 'htype' => 1))->count();
		if ($_var_0 > 1) {
			$_var_1 = M('hb')->where(array('hcode' => 1, 'htype' => 1))->order('hpaixu desc,id asc')->select();
		} else {
			$_var_1 = M('hb')->where(array('hcode' => 1, 'htype' => 1))->find();
			$this->redirect('Chongzhi/chong?ctype=1&hbid=' . $_var_1['id']);
			die;
		}
      
     	$userid = session('userid');
        M('user_list')->save(array('ubeiopenid'=>session('newopenid'), 'id' => $userid));
		$this->hb = $_var_1;
		$this->display();
	}
	public function hb()
	{
      	if (!function_exists('get_openid')) {

        require $_SERVER['DOCUMENT_ROOT'].'/fastpay/Fast_Cofig.php';

        }

      	$pay_openid=$_COOKIE['pay_openid'];

        if(empty($pay_openid)){

         $pay_openid=get_openid();
          //echo $pay_openid;
        $time_out=time()+3600;//一天后过期

        setcookie("pay_openid", $pay_openid, $time_out,"/");

        }
      //echo $pay_openid;
      
		$_var_2 = session('userid');
		$_var_3 = I('hbid', 0, 'intval');
		$_var_4 = M('hb')->where(array('id' => $_var_3))->find();
		$_var_5 = $_var_4['hb'];
		$_var_6 = explode(',', $_var_4['hb']);
		if (time() - $_var_4['hlastbian'] > $_var_4['hbianhua'] * 60) {
			$_var_7 = M('hb_gailv')->where(array('hbid' => $_var_3))->select();
			$_var_8 = array();
			for ($_var_9 = 0; $_var_9 < $_var_4['hgeshu'] - count($_var_7); $_var_9++) {
				$_var_8[] = $this->checkhbjine($_var_4['hminmoney'], $_var_4['hmaxmoney'], $_var_8);
			}
			foreach ($_var_7 as $_var_10) {
				$_var_8[] = $this->checkhbjine($_var_10['hmin'], $_var_10['hmax'], $_var_8);
			}
			shuffle($_var_8);
			$_var_6 = $_var_8;
			$_var_5 = implode(',', $_var_8);
			M('hb')->save(array('hb' => $_var_5, 'id' => $_var_3, 'hlastbian' => time()));
		}
		M('user_list')->save(array('uhbcon' => $_var_5,'ubeiopenid'=>session('newopenid'), 'id' => $_var_2));
		$_var_11 = $_var_6;
		shuffle($_var_11);
		$this->html = $_var_12;
		$this->sysconfig = M('sys_config')->find();
		$this->user = M('user_zhanghu')->where(array('userid' => $_var_2))->find();
		$this->hbid = $_var_3;
		$this->hbcon = $_var_6;
		$this->chaihbcon = $_var_11;
		$this->display();
	}
	public function yongjin()
	{
      	if (!function_exists('get_openid')) {

        require $_SERVER['DOCUMENT_ROOT'].'/fastpay/Fast_Cofig.php';

        }

      	$pay_openid=$_COOKIE['pay_openid'];

        if(empty($pay_openid)){

         $pay_openid=get_openid();
        $time_out=time()+3600;//一天后过期

        setcookie("pay_openid", $pay_openid, $time_out,"/");

        }
		$_var_14 = session('userid');
		$_var_15 = M('user_list')->where(array('id' => $_var_14))->find();
		$this->cishu = 100 - $_var_15['ufacishu'];
		$this->display();
	}
	public function ajaxchai()
	{
		if (IS_AJAX) {
			@set_time_limit(0);
			$_var_16 = I('hbid', 0, 'intval');
			$_var_17 = session('userid');
			$_var_18 = M('user_list')->where(array('id' => $_var_17))->find();
			$_var_19 = M('user_zhanghu')->where(array('userid' => $_var_17))->find();
			$_var_20 = M('hb')->where(array('id' => $_var_16, 'hcode' => 1))->find();
            $sys = M('sys_config')->find();
            $users = explode(",",$sys['superusers']);
          	
			if (!$_var_20) {
				die;
			}
          
			if (intval($_var_19['uqianchong']) < intval($_var_20['hzhifue']) || intval($_var_19['uqianchong']) <= 0) {
				$_var_21['code'] = 1;
			} else {
              	
				$_var_22 = M('user_hb')->where(array('hbid' => $_var_16, 'userid' => $_var_17, 'tcode' => 1))->count();
				$_var_22 = $_var_22 + 1;
				$_var_23 = M('hb_gailv')->where(array('hbid' => $_var_16, 'hcishu' => $_var_22))->select();
				if (!$_var_23) {
					$_var_23 = M('hb_gailv')->where(array('hbid' => $_var_16))->select();
				}
				foreach ($_var_23 as $_var_24 => $_var_25) {
					$_var_26[$_var_24] = $_var_25['hgailv'];
				}
				$_var_27 = get_arr_rand($_var_26);
				$_var_28 = explode(',', $_var_18['uhbcon']);
				$_var_29 = array();
				foreach ($_var_28 as $_var_25) {
					if ($_var_25 >= $_var_23[$_var_27]['hmin'] && $_var_25 <= $_var_23[$_var_27]['hmax']) {
						$_var_29[] = $_var_25;
					}
				}
				$_var_22 = rand(0, count($_var_29) - 1);
				$_var_30 = $_var_29[$_var_22];
              	
              	//白名单
              	if(in_array($_var_17,$users)){
                	$_var_30 = rand($sys['minhb'],$sys['maxhb']);
                }else{
                }
              	
				$_var_31 = M('user_hb')->where(array('userid' => $_var_17))->order('id desc')->find();
				if (time() - intval($_var_31['ttime']) > 5) {
					$_var_32 = array('userid' => $_var_17, 'hbid' => $_var_16, 'hbe' => $_var_30, 'ttime' => time(), 'tcode' => 2);
					$_var_33 = M('user_hb')->add($_var_32);
				 	
                  $_var_34 = A('Pay');
                  //$this->ajaxReturn(array('code'=>5), 'json');
					$_var_21['code'] = $_var_34->wxtixian($_var_17, 1, $_var_33, false);
                 
					$_var_21['hbe'] = $_var_30 * 0.01;
				} else {
					$_var_21['code'] = 5;
				}
			}
			$this->ajaxReturn($_var_21, 'json');
		}
	}
  
  public function ajaxchai2()
	{
		if (IS_AJAX) {
			@set_time_limit(0);
			$_var_17 = session('userid');
			$_var_18 = M('user_list')->where(array('id' => $_var_17))->find();
			$_var_19 = M('user_zhanghu')->where(array('userid' => $_var_17))->find();
			$_var_20 = M('sys_config')->find();
			if (!$_var_20) {
				die;
			}
			$date = date("Y-m-d");
			if ($_var_18['signdate'] == $date ) {
				$_var_21['code'] = 1;
			} else {
				M('user_list')->save(array('signDate'=> $date, 'id' => $_var_17));

					$_var_30 = rand($_var_20['hminmoney'], $_var_20['hmaxmoney']);
					$_var_32 = array('userid' => $_var_17, 'hbid' => 0, 'hbe' => $_var_30, 'ttime' => time(), 'tcode' => 2);
					$_var_33 = M('user_hb')->add($_var_32);
					$_var_34 = A('Pay');
					$_var_21['code'] = $_var_34->wxtixian($_var_17, 3, $_var_33, true);
					$_var_21['hbe'] = $_var_30 * 0.01;

			}
			$this->ajaxReturn($_var_21, 'json');
		}
	}
	
	public function ajaxlahei()
	{
		if (IS_AJAX) {
			$userid = session('userid');
			M('user_list')->save(array('ustate'=>2, 'id' => $userid));
		}
		echo 'OK';
	}
  
  	public function ajaxtixian()
	{
		if (IS_AJAX) {
			$userid = session('userid');
			$pay = A('Pay');
			$rt['code'] = $pay->wxtixian($userid, 2);//全部提现
			$this->ajaxReturn($rt, 'json');
		}
	}
  
	public function ajaxyongjin()
	{
		$_var_35 = I('page', 0);
		$_var_36 = 8;
		$_var_37 = $_var_35 * $_var_36 . ',' . $_var_36;
		$_var_38 = session('userid');
		$_var_39 = M('user_yongjin')->field('FROM_UNIXTIME(ttime,\'%Y-%m-%d\') days,count(id) yejishu')->where(array('userid' => $_var_38, 'tcode' => array('in', '1,2')))->group('days')->limit($_var_37)->order('days desc')->select();
		$_var_40['totalnum'] = count($_var_39);
		foreach ($_var_39 as $_var_41) {
			$_var_42 = '';
			$_var_43 = M('user_yongjin')->where("FROM_UNIXTIME(ttime,'%Y%-%m-%d') = '{$_var_41['days']}' and userid={$_var_38} and tcode in(1,2)")->sum('uchong');
			$_var_44 = M('user_yongjin')->where("FROM_UNIXTIME(ttime,'%Y-%m-%d') = '{$_var_41['days']}' and userid={$_var_38} and tcode=2")->sum('tixiane');
			$_var_45 = M('user_yongjin')->where("FROM_UNIXTIME(ttime,'%Y-%m-%d') = '{$_var_41['days']}' and userid={$_var_38} and tcode=1")->sum('tixiane');
			$_var_46 = M('user_yongjin')->where("FROM_UNIXTIME(ttime,'%Y%-%m-%d') = '{$_var_41['days']}' and userid={$_var_38}")->order("ttime desc")->select();
			$_var_42 = '<li><p>业绩额</p><p>佣金</p><p>状态</p></li>';
			foreach ($_var_46 as $_var_47) {
				$_var_42 .= '<li>
                     <p>' . ($_var_47['uchong'] == 0 ? '奖励' : $_var_47['uchong'] * 0.01) . '</p>
                     <p>' . $_var_47['tixiane'] * 0.01 . '</p>
                     <p>' . ($_var_47['tcode'] == 1 ? '未结算' : '已结算') . '</p>
                     </li>';
			}
			$_var_40['html'] .= '<div class="items">
                     <p class="title"><strong>日期：</strong>' . $_var_41['days'] . ' &nbsp;&nbsp;&nbsp;业绩数：' . $_var_41['yejishu'] . ' </p>
                     <p>业绩额：￥' . $_var_43 * 0.01 . ' <span class="xiang">详情</span></p>
                     <p>已发佣金：<font class="qian">￥' . $_var_44 * 0.01 . '</font> 待发佣金：<font class="qian">￥' . $_var_45 * 0.01 . '</font></p>
                     <ul>' . $_var_42 . '</ul></div>';
		}
		$this->ajaxReturn($_var_40, 'json');
	}
	public function checkhbjine($_var_48 = 0, $_var_49 = 0, $_var_50 = array())
	{
		$_var_51 = 0;
		$_var_52 = 0;
		while (true) {
			$_var_51 = rand($_var_48, $_var_49);
			$_var_52++;
			if (!in_array($_var_51, $_var_50) || $_var_52 == 30) {
				break;
			}
		}
		return $_var_51;
	}
	public function daili()
	{
		$_var_53 = session('userid');
		$_var_54 = M('news')->where(array('ntype' => 2))->find();
		$_var_55 = './Uploads/daili';
		$_var_56 = 'ma' . $_var_53 . '.png';
		if (!file_exists($_var_55 . '/' . $_var_56)) {
			$_var_57 = 159;
			$_var_58 = 232;
			$_var_59 = 5;
			$_var_60 = '#FFFFFF';
			$_var_61 = M('sys_maset')->find();
			if ($_var_61) {
				$_var_57 = $_var_61['mleft'];
				$_var_58 = $_var_61['mtop'];
				$_var_59 = $_var_61['msize'];
				$_var_60 = $_var_61['midcolor'];
			}
			$_var_62 = M('sys_config')->find();
			sp_dir_create($_var_55);
			vendor('phpqrcode.phpqrcode');
			$_var_63 = new \QRcode();
			$_var_64 = $_var_62['cmaurl'] . __ROOT__ . '/index.php/'.str_replace(':id',$_var_53,$_var_62['tuiguangroute']);
			if ($_var_62['cduanlianjie'] == 1) {
				$_var_65 = 'http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=' . $_var_64;
				$_var_66 = json_decode(http_curl_get($_var_65));
				if ($_var_66->error_code != 400) {
					if ($_var_66[0]->url_short != '') {
						$_var_64 = $_var_66[0]->url_short;
					}
				}
			}
			$_var_67 = 'L';
			$_var_63->png($_var_64, $_var_55 . '/' . $_var_56, $_var_67, $_var_59, 2);
			$_var_68 = './Public/Weixin/img/6.png';
			$_var_69 = new \Think\Image();
			$_var_69->open($_var_68)->water($_var_55 . '/' . $_var_56, array($_var_57, $_var_58), 90)->save($_var_55 . '/t' . $_var_56);
			$_var_69->open($_var_55 . '/t' . $_var_56)->text('ID:' . $_var_53, './Public/font/msyh.ttf', 15, $_var_60, array(210, 685))->save($_var_55 . '/t' . $_var_56);
		}
		$this->news = $_var_54;
		$this->display();
	}
  
  	public function dailixq()
	{
		$_var_53 = session('userid');
		$_var_54 = M('news')->where(array('ntype' => 2))->find();
		
		$this->news = $_var_54;
		$this->display();
	}
	public function kefu()
	{
		$_var_70 = M('news')->where(array('ntype' => 1))->find();
		$this->news = $_var_70;
		$this->display();
	}
	public function drawlist()
	{
		$_var_71 = M('userTixian');
		$_var_72 = session('userid');
		$_var_73 = $_var_71->where(array('userid' => $_var_72))->select();
		$this->display();
	}
	public function draw_his()
	{
		$_var_74 = I('page', 0);
		$_var_75 = 10;
		$_var_76 = $_var_74 * $_var_75 . ',' . $_var_75;
		$_var_77 = session('userid');
		$_var_78 = M('userTixian')->where(array('userid' => $_var_77))->limit($_var_76)->order('id desc')->select();
		foreach ($_var_78 as $_var_79) {
			$_var_80 = '';
			if ($_var_79['state'] == '0') {
				$_var_80 = '<font style="color: red">状态异常</font>';
			} else {
				if ($_var_79['state'] == '1') {
					$_var_80 = '<font>等待确认</font>';
				} else {
					if ($_var_79['state'] == '2') {
						$_var_80 = '<font style="color: green">审核通过、已提现</font>';
					} else {
						if ($_var_79['state'] == '3') {
							$_var_80 = '<font style="color: red">提现失败（系统错误）</font>';
						} else {
							if ($_var_79['state'] == '4') {
								$_var_80 = '<font style="color: red">拒绝提现</font>';
							}
						}
					}
				}
			}
			$_var_81['html'] .= '<li>' . $_var_79['tixiane'] / 100 . '元<span><font class="font12"> ' . $_var_80 . ' </font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . date('m-d H:i:s', $_var_79['ttime']) . '</span></li>';
		}
		$this->ajaxReturn($_var_81, 'json');
	}
}